import { _decorator, Component, director, sys } from 'cc';
import { SceneMgr } from '../../Framework/Scripts/Managers/SceneMgr';
import { MsgData } from './Data/MsgData';
import { GameData } from './Data/GameData';
import { CocosUtils } from '../../Framework/Scripts/Utils/CocosUtils';
import { NetConfig, WSConnectedEvent } from '../../Framework/Scripts/Managers/Net/NetCfg';
import { Stype } from './Config/GameCfg';
import { NetCfg } from '../AssetPackage/Props/Login/Login';
const { ccclass, property } = _decorator;

@ccclass('GameApp')
export class GameApp extends Component {

    public static Instance: GameApp = null!;
    ret_func: any;
    protected onLoad(): void {
        if(GameApp.Instance !== null) {
            this.destroy();
            return;
        }
        GameApp.Instance = this;
    }

    start() {
        // director.on(WSConnectedEvent,this.onNetConnect,this);
    }

    protected onDestroy(): void {
        // director.off(WSConnectedEvent,this.onNetConnect,this);
    }

    public Init(){

    }

    public connectNetServer(type, ret_func:Function = null){
        SceneMgr.inst.gameView = type;
        let cfg:NetCfg = {
            ip: '',
            port: 0
        };
        switch (type) {
            case Stype.Auth:
                {
                    cfg.ip = NetConfig.ip;
                    cfg.port= NetConfig.port;
                    let url = CocosUtils.getUrlByIdAndPort(cfg.ip,cfg.port);
                    this.connectWebSocket(url);
                }
                break;
            case Stype.Hall:
                {
                    cfg.ip = GameData.inst.hallNetCfg.ip;
                    cfg.port = GameData.inst.hallNetCfg.port;
                    let url = CocosUtils.getUrlByIdAndPort(cfg.ip,cfg.port);
                    this.connectWebSocket2(url);
                    this.ret_func = ret_func;
                }
                break;
            case Stype.Game:
                {
                    cfg.ip = GameData.inst.gameNetCfg.ip;
                    cfg.port = GameData.inst.gameNetCfg.port;
                    let url = CocosUtils.getUrlByIdAndPort(cfg.ip,cfg.port);
                    this.connectWebSocket2(url);
                    this.ret_func = ret_func;
                }
                break;
            default:
                break;
        }


    }

    //链接网络成功
    public onNetConnect(calBack:Function){
        let des = {
            1: "登录服务",
            2:'大厅服务',
            3:'游戏服务'
        }
        console.warn('==========GameApp onNetConnect==============',des[SceneMgr.inst.gameView])
        if(SceneMgr.inst.gameView == Stype.Auth){
            GameApp.Instance.EnterGame();
        }else if(SceneMgr.inst.gameView == Stype.Hall){
            SceneMgr.inst.LoadingToHall();
            if(this.ret_func){
                // UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameMRTZ/GameMRTZ',UIType.Full); 
                this.ret_func()
            } 
        }else if(SceneMgr.inst.gameView == Stype.Game){
            if(this.ret_func){
                this.ret_func()
            } 
        }
    }

    public EnterGame(){
        //初始化网络解析类
        MsgData.inst.Init();
        //加载大厅资源
        SceneMgr.inst.loadHallRes();
    }
    
    //连接网络 登录的链接websocket，不需要完毕再链接
    public connectWebSocket(url: string){
        // if(true){
        //     url += '/dt';
        // }
        // console.log('=======connectWebSocket============',url);
        // let rootNode = director.getScene().getChildByName('__rootNode__');
        // let platform = sys.platform;
        // if(platform === sys.Platform.WECHAT_GAME){
        //     //使用微信的websocket
        //     rootNode.addComponent(WsWXNetMgr).Init();
        //     WsWXNetMgr.Instance.ConnectToServer();
        // }else{
        //     // rootNode.addComponent(WsNetMgr).Init()
        //     // WsNetMgr.Instance.ConnectToServer(url);
        // }
    }

    //有链接的时候断掉原来的链接
    public connectWebSocket2(url: string){
        // if(true){
        //     url += '/dt';
        // }
        // console.log('=======connectWebSocket============',url);
        // let platform = sys.platform;
        // if(platform === sys.Platform.WECHAT_GAME){
        //     //使用微信的websocket 暂时未处理
        //     WsWXNetMgr.Instance.ConnectToServer();
        // }else{
        //     WsNetMgr.Instance.CloseSocket(()=>{
        //         console.log('=======connectWebSocket2==CloseSocket==========')
        //         WsNetMgr.Instance.ConnectToServer(url);
        //     });
        // }
    }
}
